﻿using UnityEngine;
using System.Collections;

public class test2 : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    void OnGUI()
    {
        if (GUILayout.Button("Cube_Green new WWW"))
        {
            StartCoroutine("LoadCubeGreen");
        }
        else if (GUILayout.Button("Cube_Green2 LoadFromCacheOrDownload"))
        {
            StartCoroutine("LoadCubeGreen2");
        }
        else if (GUILayout.Button("Cube_White LoadFromFile"))
        {
            StartCoroutine("LoadCubeWhite");
        }
    }

    IEnumerator LoadCubeGreen()
    {
        string mUrl = "file:///" + Application.streamingAssetsPath + "/Windows/" + "test2/cube_green.unity3d";
        //string mUrl = Application.streamingAssetsPath + "/Windows/" + "test2/cube_green.unity3d";
        WWW mwww = new WWW(mUrl);
        yield return mwww;
        if (!string.IsNullOrEmpty(mwww.error))
        {
            Debug.Log(mwww.error);
        }
        else
        {
            AssetBundle bundle = mwww.assetBundle;
            if (bundle != null)
            {
                Object o = bundle.LoadAsset("Cube_Green");
                GameObject gobj = Instantiate(o) as GameObject;
                gobj.transform.position = Vector3.one;

            }

            //AssetBundle mab = mwww.assetBundle;
            //AssetBundleManifest mainfest = (AssetBundleManifest)mab.LoadAsset("AssetBundleManifest");
            //mab.Unload(false);

        }
    }

    IEnumerator LoadCubeGreen2()
    {
        string mUrl = "file:///" + Application.streamingAssetsPath + "/Windows/" + "test2/cube_green2.unity3d";
        //string mUrl = Application.streamingAssetsPath + "/Windows/" + "test2/cube_green.unity3d";
        WWW mwww = WWW.LoadFromCacheOrDownload(mUrl, 0);
        yield return mwww;
        if (!string.IsNullOrEmpty(mwww.error))
        {
            Debug.Log(mwww.error);
        }
        else
        {
            AssetBundle bundle = mwww.assetBundle;
           if(bundle != null)
            {
                Object o =  bundle.LoadAsset("Cube_Green2");
                GameObject gobj = Instantiate(o) as GameObject;
                gobj.transform.position = new Vector3(-1,0,0);
                    
            }

            //AssetBundle mab = mwww.assetBundle;
            //AssetBundleManifest mainfest = (AssetBundleManifest)mab.LoadAsset("AssetBundleManifest");
            //mab.Unload(false);

        }
    }

    IEnumerator LoadCubeWhite()
    {
        //string mUrl = "file:///" + Application.streamingAssetsPath + "/Windows/" + "test2/cube_green2.unity3d";
        string mUrl = Application.streamingAssetsPath + "/Windows/" + "test2/cube_white.unity3d";

        AssetBundle bundle = AssetBundle.LoadFromFile(mUrl);
        if (bundle != null)
        {
            Object o = bundle.LoadAsset("Cube_White");
            GameObject gobj = Instantiate(o) as GameObject;
            gobj.transform.position = new Vector3(2, 0, 0);

        }
        yield break;
    }

    IEnumerator LoadCubeWhite2()
    {
        //string mUrl = "file:///" + Application.streamingAssetsPath + "/Windows/" + "test2/cube_green2.unity3d";
        string mUrl = Application.streamingAssetsPath + "/Windows/" + "test2/cube_white.unity3d";
        /*
        AssetBundle bundle = AssetBundle.LoadFromMemory(mUrl);
        if (bundle != null)
        {
            Object o = bundle.LoadAsset("Cube_Green2");
            GameObject gobj = Instantiate(o) as GameObject;
            gobj.transform.position = new Vector3(-1, 0, 0);

        }*/
        yield break;
    }

}
